package com.example.fengyungo.controller.user;

import com.example.fengyungo.result.Result;
import com.example.fengyungo.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

// TODO: 2023/12/12 测试使用记得删
@RestController
@RequestMapping("webSocket")
public class WebSocketController {

    @Autowired
    WebSocketServer webSocketServer;

    /**
     * 广播
     *
     * @param message 内容
     * @return Result
     */
    @GetMapping("/all/{message}")
    public Result sendAll(@PathVariable String message) {
        webSocketServer.sendToAllClient(message);
        return Result.success();
    }

    /**
     * 发送给特定的客户端
     *
     * @param sid     指定客户端的sid
     * @param message 消息
     * @return Result
     */
    @GetMapping("/single/{sid}/{message}")
    public Result sendSingle(@PathVariable String sid, @PathVariable String message) {
        webSocketServer.sendToClient(sid, message);
        return Result.success();
    }
}
